<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Session;
use App\Services\MemberService;
use Illuminate\Support\Facades\Redis;

class WeChatsController extends Controller{
    public function getcode($uri=''){
        $canshu = "";
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize";
        $canshu = $canshu . "?appid=wx8bce72636ba79f5e";
        // $canshu = $canshu . "&redirect_uri=".urlencode("http://boomdog.madao100.com/get_token_openid/{$uri}");
        if($uri != ''){
            $_url = "http://boomdog.madao100.com/get_token_openid/".$uri;
        }else{
            $_url = "http://boomdog.madao100.com/get_token_openid";
        }

        $canshu = $canshu . "&redirect_uri=".urlencode($_url);
        $canshu = $canshu . "&response_type=code";
        $canshu = $canshu . "&scope=snsapi_userinfo";
        $canshu = $canshu . "&state=123#wechat_redirect";
        $url = $url . $canshu;

        header("Location:$url");
    }

    public function getopenid($uri=''){
        $code = $_REQUEST['code'];

        if($uri != ''){
            $uri_list = explode('_2f',$uri);
        }

        if(!empty($uri_list)){
            if(in_array('courseinfo',$uri_list)){
                $_uri = '#/curriculumDetail?id='.array_pop($uri_list);
            }

            if(in_array('activity',$uri_list)){
                $_uri = '#/courseDetails?id='.array_pop($uri_list);
            }

            if(in_array('contentinfo',$uri_list)){
                $_uri = '#/play?id='.array_pop($uri_list);
            }
        }else{
            $_uri = '#home';
        }


        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx8bce72636ba79f5e&secret=c98e8c75b8a31765e6745b8cc24a4180&code=".$code."&grant_type=authorization_code";
        $info = file_get_contents($url);
        $info = json_decode($info,1);

        $access_token = $info['access_token'];
        $openid = $info['openid'];
        $userurl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        $userinfo = file_get_contents($userurl);
        $userinfo = json_decode($userinfo,1);

        $member = new MemberService;
        $user_info = $member->getMemberInfoByOpenId($userinfo['openid']);

        if(!empty($user_info)){
            $param = array('access_token'=>$access_token,'openid'=>$openid,'code'=>$code,'id'=>$user_info->id);
            $_code = base64_encode(json_encode($param));
            $_last_str = substr($_code,-1);
            if($_last_str == '='){
                $_last_str = '_3d';
            }
            $_code = str_replace('=',$_last_str,$_code);

            if(strpos($_uri,'?') !== FALSE){
                $_uri .= '&code='.$_code;
            }else{
                $_uri .= '?code='.$_code;
            }

            // $_code = urlencode($_code);
            $_data = array('code'=>'3x000001','msg'=>'用户已注册','_uri'=>'http://boom.madao100.com/'.$_uri);
            // $_data = array('code'=>'3x000001','msg'=>'用户已注册','_uri'=>'http://boom.madao100.com/#home?code='.$_code);
            $member->modifyMember($userinfo->id,['login_time'=>date('Y-m-d H:i:s')]);
            header('Location:'.urldecode($_data['_uri']));
        }else{
            preg_match('/[\x{4e00}-\x{9fa5}A-Za-z0-9_]+/u', $userinfo['nickname'],$matches);

            $userinfo['nickname'] = base64_encode($userinfo['nickname']);
            $data['telephone'] = '';
            $data['isbuy'] = 0;
            $data['check'] = 'normal';
            $data['nick'] = $userinfo['nickname'];
            $data['thumb'] = $userinfo['headimgurl'];
            $data['register_date'] = date('Y-m-d H:i:s');
            $data['openid'] = $userinfo['openid'];

            $res = $member->insertMember($data);
            if($res){
                $param = array('access_token'=>$access_token,'openid'=>$userinfo['openid'],'code'=>$code,'id'=>$res);
                $_code = base64_encode(json_encode($param));

                $_last_str = substr($_code,-1);
                if($_last_str == '='){
                    $_last_str = '_3d';
                }
                $_code = str_replace('=',$_last_str,$_code);

                if(strpos($_uri,'?' !== FALSE)){
                    $_uri .= '&code='.$_code;
                }else{
                    $_uri .= '?code='.$_code;
                }
                // $_code = urlencode($_code);
                $_data = array('code'=>'2x000001','msg'=>'注册成功','_uri'=>'http://boom.madao100.com/'.$_uri);
                // $_data = array('code'=>'3x000001','msg'=>'用户已注册','_uri'=>'http://boom.madao100.com/#home/?code='.$_code);
                $userinfo = array(
                    'id'=>$res,
                    'nick'=>$userinfo['nickname'],
                    'thumb'=>$userinfo['headimgurl'],
                    'openid'=>$userinfo['openid'],
                    'check'=>'normal',
                    'isbuy'=>0,
                    'telephone'=>'',
                    'register_date'=>date('Y-m-d H:i:s')
                );

                header('Location:'.urldecode($_data['_uri']));
            }else{
                $_data = array('code'=>'4x000001','msg'=>'注册失败');
                exit(json_encode($_data));
            }
        }
    }
}
